<%@ page language="java" import="java.util.*"  pageEncoding="utf-8"%>
<%@include file="common/tag.jsp"%>
<c:set var="CTX" value="${pageContext.request.contextPath}" />
<%@include file="common/indexhead.jsp"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
    
    <title>采购订货单新增页</title>
    </head>

 <body>
<div class="panel panel-primary">
	<div class="panel-heading">
		<div>
			<h3 class="panel-title">采购订货单新增页</h3>
		</div>
		<div class="button"
			style="float: right;margin-top: -27px;margin-right: 5px;">
			<a class="btn btn-info" href="#" onclick="javascript:window.close();"><i class="fa fa-sign-out"></i>退出</a>
		</div>
		<div class="button"
			style="float: right;margin-top: -27px;margin-right: 5px;">
			<div class="btn btn-info"   onclick="tijiao()" href=""><i class="fa fa-check"></i>保存</div>
		</div>
	</div>
	<div class="panel-body" style="height:87%">
			
			
			<form action="/purchase/adddo" method="post"  id="purchaseadd"  class="form-horizontal"
			style="width:95%;margin-top: 50px;">
			<fieldset>
			<table class="table table-bordered">
				<div class="form-group">
					<label class="col-sm-1 control-label">订单编号</label>
					<div class="col-sm-3">
						<input readOnly=true class="form-control" id="purchaseDdbh" name="purchaseDdbh" type="text"
						placeholder = "${number}" value = "${number}"/>
					</div>
					<label class="col-sm-1 control-label">结算方式</label>
					<div class="col-sm-3">
						<select class="form-control" id="purchaseJsfs" name="purchaseJsfs">
            				<option value="现金结算">现金结算</option>
            				<option value="银行转账">银行转账</option>
        				</select>
					</div>
					<label class="col-sm-1 control-label">到货日期</label>
					<div class="col-sm-3">
						<input readOnly=true class="form-control mydate" id="purchaseDdrq" name="purchaseDdrq" type="text"/>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-1 control-label">供应商</label>
					<div class="col-sm-3">
						<input readOnly=true style="width:55%;display: inline-block;" class="form-control" id="purchaseGys" name="purchaseGys" type="text"/>
						<input style="display:none" class="form-control" id="purchaseGysId" name="purchaseGysId" type="text"/>
						<a class="btn btn-info"  onclick="yongan.dialog.showdialog_gys('/util/businesstree','deptdialog','purchaseGys','purchaseGysId','purchaseLxr','purchaseLxdh');"><i class="fa fa-sign-out"></i>选供应商</a>
					</div>
					<label class="col-sm-1 control-label">联系人</label>
					<div class="col-sm-3">
						<input readOnly=true class="form-control" id="purchaseLxr" name="purchaseLxr" type="text"/>
					</div>
					<label class="col-sm-1 control-label">联系电话</label>
					<div class="col-sm-3">
						<input readOnly=true class="form-control" id="purchaseLxdh" name="purchaseLxdh" type="text"/>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-1 control-label">预付金额</label>
					<div class="col-sm-3">
						<input class="form-control" id="purchaseYfdj" name="purchaseYfdj" type="text"/>
					</div>
					<label class="col-sm-1 control-label">收货仓库</label>
					<div class="col-sm-3">
						<input readOnly=true style="width:55%;display: inline-block;" class="form-control" id="purchaseShck" name="purchaseShck" type="text"/>
						<input style="display:none" class="form-control" id="purchaseShckId" name="purchaseShckId" type="text"/>
						<a class="btn btn-info"  onclick="yongan.dialog.showdialog('/util/warehousetree','deptdialog','purchaseShck','purchaseShckId');"><i class="fa fa-sign-out"></i>选择仓库</a>
					</div>
					<label class="col-sm-1 control-label">采购员</label>
					<div class="col-sm-3">
						<input readOnly=true style="width:55%;display: inline-block;" class="form-control" id="purchaseCgy" name="purchaseCgy" type="text"/>
						<input style="display:none" class="form-control" id="purchaseCgyId" name="purchaseCgyId" type="text"/>
						<a class="btn btn-info"  onclick="yongan.dialog.showdialog('/util/usertree','deptdialog','purchaseCgy','purchaseCgyId');"><i class="fa fa-sign-out"></i>选择人员</a>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-1 control-label">到货地点</label>
					<div class="col-sm-11">
						<input class="form-control" id="purchaseDddd" name="purchaseDddd" type="text"/>
					</div>
				</div>
				</table>
			</fieldset>
			<hr></hr>
<table class="table table-bordered">
  <caption style="width:90%">采购货品明细
  <p style="float:right;width:50%;text-align:right">
  <a  onclick="addTable();" class="btn btn-info">增加商品</a>
  <a  onclick="del();" class="btn btn-danger">批量删除</a>
  </p></caption>
  <thead>
    <tr>
	  <th style="width:3%" rowspan="2"><input type='checkbox'  id='checkAll' onclick="checkAll_f()" value='New'></th>
      <th colspan="3">货品信息</th>
      <th rowspan="2">单位</th>
      <th rowspan="2">数量</th>
      <!-- <th rowspan="2">出库数量</th> -->
      <th rowspan="2">单价</th>
      <th rowspan="2">总金额</th>
      <th rowspan="2">备注</th>
      <th rowspan="2">到货日期</th>
      <th rowspan="2">操作</th>
    </tr>
    <tr>
      <th>货品编码</th>
      <th>货品名称</th>
      <th>规格型号</th>
    </tr>
  </thead>
  <tbody id="addTable">
  </tbody>
</table>
		</form>
		<hr></hr>
		
			<style>
.table >thead> tr> th{
	text-align:center;
	vertical-align: middle;
	background-color: #DDD9D4;
	border-color:#BEC2C1;
	padding-left: 0px;
}
.table >tbody> tr> td>input{
	width:100%;
}
.table >tbody> tr> td {
	text-align:center;
	width:8%
}
</style>

	</div>
</div>
<div id="deptdialog">
</div>
<!-- 弹出框重写 -->
<div id="ycf-alert" class="modal">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
            <h5 class="modal-title"><i class="fa fa-exclamation-circle"></i> [Title]</h5>
          </div>
          <div class="modal-body small">
            <p>[Message]</p>
          </div>
          <div class="modal-footer" >
            <button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>
            <button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>
          </div>
        </div>
      </div>
    </div>
<!-- 弹出框重写end -->
<%@include file="common/indexbottom.jsp"%>
<script type="text/javascript">
var row_count = 0; 
function addTable(){
	var table1 = $("#addTable");
	row_count++; 
	//var table1 = $('#table1'); 
	var firstTr = table1.find('tbody>tr:first'); 
	var row = $("<tr></tr>"); 
	var tdsc = $("<td></td>");
	var tdxh = $("<td style='width:3%'></td>"); 
	tdxh.append($("<input type='checkbox' name='count' value='New'>")); 
	row.append(tdxh); 
	//for(i=0;i<10;i++){
		// var td = $("<td></td>"); 
		 //row.append("<td style='display:none'><input class='form-control'  type='text' name='details["+(row_count-1)+"].outgooddetailsId' ></td>") ;
		 row.append("<td><input readonly=true onclick='checkhpxx(this)' class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpbm' ></td>") ;
		 row.append("<td><input readonly=true class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpmc' ></td>") ;
		 row.append("<td><input readonly=true class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsGgxh' ></td>") ;
		 row.append("<td><input readonly=true class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpdw' ></td>") ;
		 row.append("<td><input class='form-control' onchange='zongjine(this)'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpsl' ></td>") ;
		 row.append("<td><input readonly=true class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpdj' ></td>") ;
		 row.append("<td><input readonly=true class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsHpzje' ></td>") ;
		 row.append("<td><input class='form-control'  type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsBz' ></td>") ;
		 row.append("<td><input readOnly=true class='form-control mydate' id='countdate"+row_count+"' type='text' name='details["+(row_count-1)+"].purchasegoodsdetailsDhrq' ></td>") ;
		 //row.append(td); 
	//}
	var tdsc = $("<td></td>");
	tdsc.append("<input type='button' value='删除' class='btn btn-danger' onclick='delRow(this)'>") ;
	row.append(tdsc); 
	table1.append(row); 
	$( ".mydate" ).datepicker();
}
function zongjine(obj){
	var shuliang = obj.value;
	var name = $(obj)[0].name;
	var indexx = parseInt(name.substring(name.indexOf("[")+1,name.indexOf("]")));
	var danjianame = "details["+indexx+"].purchasegoodsdetailsHpdj";
	var zongjinename = "details["+indexx+"].purchasegoodsdetailsHpzje";
	var danjiavalue = $("input[name='"+danjianame+"']").val();
	if(yongan.zZs((shuliang))){
		if(danjiavalue==""|shuliang==""){
			alert("单价或数量为空，无法计算");
			$("input[name='"+zongjinename+"']").val(0);
			return false;
		}
		var zongjinevalue = parseFloat(shuliang)*parseFloat(danjiavalue);
		$("input[name='"+zongjinename+"']").val(zongjinevalue);
	}else{
		obj.value="";
		$("input[name='"+zongjinename+"']").val(0);
	}
	
}
function checkhpxx(obj){
	var name = $(obj)[0].name;
	var indexx = parseInt(name.substring(name.indexOf("[")+1,name.indexOf("]")));
	var value1 = "details["+indexx+"].purchasegoodsdetailsHpbm";
	var value2 = "details["+indexx+"].purchasegoodsdetailsHpmc";
	var value3 = "details["+indexx+"].purchasegoodsdetailsGgxh";
	var value4 = "details["+indexx+"].purchasegoodsdetailsHpdw";
	var value5 = "details["+indexx+"].purchasegoodsdetailsHpdj";
	yongan.dialog.showdialog_hpxx('/util/goodsinformationtree','deptdialog',value1,value2,value3,value4,value5);
}

function delRow(obj){ 
	$(obj).parent().parent().remove(); 
	row_count--;
}
function del(){ 
	var checked = $("input[type='checkbox'][name='count']"); 
	$(checked).each(function(){ 
		if($(this).prop('checked')){ //注意：此处判断不能用$(this).attr("checked")==‘true'来判断。 
			$(this).parent().parent().remove(); 
		} 
	});
	row_count=$("input[type='checkbox'][name='count']").length;
	$("#checkAll").prop('checked',false);
}
function checkAll_f(){
	var checked = $("input[type='checkbox'][name='count']");
	$(checked).each(function(){ 
		if($("#checkAll").prop('checked')){
			$(this).prop('checked',true);
		}else{
			$(this).prop('checked',false);
		}
	});
}
$(function() {
$( ".mydate" ).datepicker();
window.Modal = function() {
    var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
    var alr = $("#ycf-alert");
    var ahtml = alr.html();
    
    var _alert = function(options) {
        alr.html(ahtml); // 复原
        alr.find('.ok').removeClass('btn-success').addClass('btn-primary');
        alr.find('.cancel').hide();
        _dialog(options);

        return {
            on: function(callback) {
                if (callback && callback instanceof Function) {
                    alr.find('.ok').click(function() {
                        callback(true)
                    });
                }
            }
        };
    };

    var _confirm = function(options) {
        alr.html(ahtml); // 复原
        alr.find('.ok').removeClass('btn-primary').addClass('btn-success');
        alr.find('.cancel').show();
        _dialog(options);

        return {
            on: function(callback) {
                if (callback && callback instanceof Function) {
                    alr.find('.ok').click(function() {
                        callback(true)
                    });

                }
            }
        };
    };

    var _dialog = function(options) {
        var ops = {
            msg: "提示内容",
            title: "操作提示",
            btnok: "确定",
            btncl: "取消"
        };

        $.extend(ops, options);

        console.log(alr);

        var html = alr.html().replace(reg,
        function(node, key) {
            return {
                Title: ops.title,
                Message: ops.msg,
                BtnOk: ops.btnok,
                BtnCancel: ops.btncl
            } [key];
        });

        alr.html(html);
        alr.modal({
            width: 500,
            backdrop: 'static'
        });
    }

    return {
        alert: _alert,
        confirm: _confirm
    }

} ();
});
 
function tijiao() {
Modal.confirm({
    msg: "是否新增？"
}).on(function(e) {
	//var allOutSourcingIn = $("input[id^='count']");
	var json = "{";
	for(var q=0;q<$("#addTable tr").length;q++){
		//var allOutSourcingIn = $("input[id^='count"+(q+1)+"']");
		var allOutSourcingIn = $("#addTable tr")[q].children;
		//var allOutSourcingInDate = $("input[id^='countdate"+(q+1)+"']");
		json=json+"'details["+q+"].purchasegoodsdetailsHpbm':'"+allOutSourcingIn[1].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsHpmc':'"+allOutSourcingIn[2].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsGgxh':'"+allOutSourcingIn[3].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsHpdw':'"+allOutSourcingIn[4].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsHpsl':'"+allOutSourcingIn[5].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsHpdj':'"+allOutSourcingIn[6].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsHpzje':'"+allOutSourcingIn[7].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsBz':'"+allOutSourcingIn[8].childNodes[0].value+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsOrder':'"+q+"',";
		json=json+"'details["+q+"].purchasegoodsdetailsDhrq':'"+allOutSourcingIn[9].childNodes[0].value+"',";
	}
	json=json+"'purchaseDdbh':'"+$("#purchaseDdbh").val()+"',";
	json=json+"'purchaseJsfs':'"+$("#purchaseJsfs").val()+"',";
	json=json+"'purchaseDdrq':'"+$("#purchaseDdrq").val()+"',";
	json=json+"'purchaseGys':'"+$("#purchaseGys").val()+"',";
	json=json+"'purchaseGysId':'"+$("#purchaseGysId").val()+"',";
	json=json+"'purchaseLxdh':'"+$("#purchaseLxdh").val()+"',";
	json=json+"'purchaseLxr':'"+$("#purchaseLxr").val()+"',";
	json=json+"'purchaseYfdj':'"+$("#purchaseYfdj").val()+"',";
	json=json+"'purchaseShck':'"+$("#purchaseShck").val()+"',";
	json=json+"'purchaseShckId':'"+$("#purchaseShckId").val()+"',";
	json=json+"'purchaseCgy':'"+$("#purchaseCgy").val()+"',";
	json=json+"'purchaseCgyId':'"+$("#purchaseCgyId").val()+"',";
	json=json+"'purchaseDddd':'"+$("#purchaseDddd").val()+"'";
	json=json+"}";
	console.log(json);
	//var a="{'details[0].ingooddetailsNumber':'q','details[0].ingooddetailsHpmc':'w','details[0].ingooddetailsGgxh':'e','details[0].ingooddetailsDw':'r','details[0].ingooddetailsSl':'t','details[0].ingooddetailsRksl':'y','details[0].ingooddetailsDj':'u','details[0].ingooddetailsZje':'i','details[0].ingooddetailsBz':'o','details[0].ingooddetailsDhrq':'06/28/2018','outsourcinginRkdn': '111'}";
	
	var b=eval('(' + json+ ')');
	//console.log(b);
	//console.log(eval('(' + a+ ')'));
    $.ajax({
        type: "post",
        url: yongan.URL.purchaseAdd(),
        dataType: "json",
       //	contentType: 'application/json;charset=utf-8',
        data: b,
        success: function(data) {
            if (data['success']) {
                Modal.alert({
                    msg: data['data']['stateinfo'],
                    title: '执行结果',
                    btnok: '确定',
                    btncl: '取消'
                }).on(function(e) {
                    window.close();
                });
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
            alert(textStatus);
            alert(errorThrown);
        }
    });
});
}
</script>
  </body>
</html>